package com.example.workrecord.controller;


import com.example.workrecord.entity.CorpUserDetail;
import com.example.workrecord.exption.CheckException;
import com.example.workrecord.util.dingtalkopenapi.utils.JwtUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * @ClassName: BaseController
 * @Description BaseController
 * @Author Mr.Bu
 * @Date 2020/4/26 11:16
 * @Version: 1.0
 */

public class BaseController {

    protected void checkNull(Object obj, String msg) {
        if (obj == null || obj.equals("")) {
            throw new CheckException(msg);
        }
        if (obj instanceof List) {
            if (checkList((List) obj)) {
                throw new CheckException(msg);
            }
        }
        if (obj instanceof Map) {
            if (checkMap((Map) obj)) {
                throw new CheckException(msg);
            }
        }
    }

    protected Boolean checkList(List list) {
        if (list == null || list.size() <= 0) {
            return true;
        }
        return false;
    }

    protected Boolean checkMap(Map map) {
        if (map == null || map.size() <= 0) {
            return true;
        }
        return false;
    }

    /**
     * 鉴权 获取用户信息
     *
     * @param request
     * @return
     */
    public CorpUserDetail getUserInfo(HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        CorpUserDetail user = JwtUtils.unsign(authorization, CorpUserDetail.class);
        return user;
    }


}
